//
//  DoneListViewController.m
//  DoIT
//
//  Created by 葛欣 on 14/11/23.
//  Copyright (c) 2014年 gex. All rights reserved.
//

#import "DoneListViewController.h"
#import "TodoService.h"
#import "Todo.h"
#import "NSDate+Utils.h"

@interface DoneListViewController ()
{
    NSMutableArray *todoArray;
}
@end

@implementation DoneListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.rowHeight = 50;
    self.tableView.allowsSelection = NO; // We essentially implement our own selection
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    //
    todoArray =[NSMutableArray array];
    NSInteger weekday = [[NSDate date] weekday];
    NSDate *tmpDate = [NSDate date];
    for (int i = weekday ; i>0; i--) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObject:[tmpDate toStringByDay] forKey:@"title"];
        NSArray *todoes = [NSArray arrayWithArray:[[TodoService sharedTodoService] getAllTodoByStatus:TodoStatusDone date:tmpDate]];
        [dic setObject:todoes forKey:@"todoes"];
        [todoArray addObject:dic];
        tmpDate=[tmpDate lastDate];
    }
    NSLog(@"%@",todoArray);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return todoArray.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSDictionary *dic = [todoArray objectAtIndex:section];
    return  [dic valueForKey:@"title"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSDictionary *dic = [todoArray objectAtIndex:section];
    return [[dic objectForKey:@"todoes"] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    SWTableViewCell *cell = (SWTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil) {
        NSMutableArray *leftUtilityButtons = [NSMutableArray new];
        NSMutableArray *rightUtilityButtons = [NSMutableArray new];
        
        [leftUtilityButtons addUtilityButtonWithColor:
         [UIColor colorWithRed:0.07 green:0.75f blue:0.16f alpha:1.0]
                                                 title:@"重做"];
        [rightUtilityButtons addUtilityButtonWithColor:
         [UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f]
                                                  icon:[UIImage imageNamed:@"cross.png"]];
        
        cell = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                      reuseIdentifier:cellIdentifier
                                  containingTableView:self.tableView // Used for row height and selection
                                   leftUtilityButtons:leftUtilityButtons
                                  rightUtilityButtons:rightUtilityButtons];
        cell.delegate = self;
    }
    
    NSArray *todoes = [[todoArray objectAtIndex:indexPath.section] objectForKey:@"todoes"];
    Todo *todo= [todoes objectAtIndex:indexPath.row];
    cell.textLabel.text = todo.content;
    return cell;
}

#pragma mark - SWTableViewDelegate

- (void)swippableTableViewCell:(SWTableViewCell *)cell didTriggerLeftUtilityButtonWithIndex:(NSInteger)index {
    switch (index) {
        case 0:
        {
            // Delete button was pressed
            NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell];
            NSMutableArray *todoes = [[todoArray objectAtIndex:cellIndexPath.section] objectForKey:@"todoes"];
            Todo *todo = [todoes objectAtIndex:cellIndexPath.row];
            todo.status = [NSNumber numberWithInteger:TodoStatusDoing];
            [[TodoService sharedTodoService] modifyTodo:todo];
            [todoes removeObject:todo];
            [self.tableView deleteRowsAtIndexPaths:@[cellIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
            [[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_KEY_REDO object:self userInfo:nil];
            break;
            
        }
        case 1:
            NSLog(@"left button 1 was pressed");
            break;
        default:
            break;
    }
}

- (void)swippableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index {
    switch (index) {
        case 0:
        {
            NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell];
            NSMutableArray *todoes = [[todoArray objectAtIndex:cellIndexPath.section] objectForKey:@"todoes"];
            [[TodoService sharedTodoService] removeTodo:[todoes objectAtIndex:cellIndexPath.row]];
            [todoes removeObjectAtIndex:cellIndexPath.row];
            [self.tableView deleteRowsAtIndexPaths:@[cellIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
            break;
        }
        case 1:
        {
            NSLog(@"More button was pressed");
            UIAlertView *alertTest = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"More more more" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles: nil];
            [alertTest show];
            
            [cell hideUtilityButtonsAnimated:YES];
            break;
        }
        default:
            break;
    }
}

@end
